﻿Imports System.Console 'Just in case anyone wants to play in beep mode
Imports System.Xml

Public Class mainWindow

    'Some basic variables needed for the app

    Dim playingStatus As Boolean = False
    Dim wrongValue As Boolean
    Dim beatNum = 0
    Dim secPlaying As Integer = 0
    Dim minsPlaying As Integer = 0

    Dim mode As String

    Const defaultTempo As Integer = 40 'Tempo if the stuff is reseted.


    'Start/Stop button management

    Private Sub start_stop_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start_stop_Btn.Click

        Dim intervalTime As Double

        'Checking again if everything's ok

        If tempoTXTBox.Text = "0" Then
            MessageBox.Show("Input number must be diffrent than 0", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)

            tempoTXTBox.Text = defaultTempo 'Reset to the default data if any error occurs
            wrongValue = True 'Value of the textbox is bad, so the value is wrong. App cannot continue.
        Else
            wrongValue = False
        End If

        'If everything's allright, the button can work properly. From now there's only button click event code executed...

        If playingStatus = False And wrongValue = False Then 'Start of playing

            Dim exception As Boolean
            Try 'Catching exception in case anyone tries to fool me
                intervalTime = tempoTXTBox.Text
            Catch ex As Exception
                exception = True
            End Try

            If exception = True Then 'Code executed when exception occurs
                MessageBox.Show("Input number must be integer type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                Call resetAll()
            Else 'Code executed if exception = false

                Dim reader As New XmlTextReader("C:\test.xml")
                Do While (reader.Read())
                    reader.IsStartElement("mode")
                    mode = reader.ReadInnerXml()
                Loop
                reader.Close()

                tempoTXTBox.ReadOnly = True
                start_stop_Btn.Text = "Stop"
                playingStatus = True

                timer.Interval = (60000 / intervalTime) '60 seconds / number of intervals
                timer.Start()
                playingTimeTimer.Start()

            End If

        Else 'Stopping

            tempoTXTBox.ReadOnly = False

            start_stop_Btn.Text = "Start"
            playingStatus = False

            timer.Stop()
            playingTimeTimer.Stop()

            Call resetAll() 'Resetting everything

        End If

    End Sub

    'Timer tick and playing subroutine

    Private Sub timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick

        beatNum = beatNum + 1 'Increasing number of the beats with every timer tick.

        If mode = "audio" Then
            playAudio()
        End If

        If mode = "beep" Then
            playBeep()
        End If

        If playingStatus = False Then
            Return 'Breaking the operation, as the stop button is clicked.
        End If

    End Sub

    'Time subroutine

    Private Sub playingTimeTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles playingTimeTimer.Tick

        'Increasing the playing time with every timer tick.

        secPlaying = secPlaying + 1

        If secPlaying = 60 Then
            minsPlaying = minsPlaying + 1
            secPlaying = 0
        End If

        If minsPlaying = 60 Then
            minsPlaying = 0
        End If

        timeTXTBox.Text = minsPlaying & ":" & secPlaying

        If playingStatus = False Then
            Return 'Breaking the operation as the stop button is clicked.
        End If

    End Sub

    'About

    Private Sub aboutBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles aboutBtn.Click
        about.ShowDialog()
    End Sub

    'Resetting everything to defaults

#Region "Playing sounds subroutines"

    'Beep mode sub
    Private Sub playBeep()

        If beatNum <> 4 Then 'Every beat except the final one
            beatProgressBar.Increment(1)
            Console.Beep(310, 250)
        Else 'Action performed only for the final beat
            beatProgressBar.Value = 0
            beatNum = 0
            Console.Beep(1000, 250)
        End If

    End Sub

    'Audio mode sub
    Private Sub playAudio()

        If beatNum <> 4 Then
            beatProgressBar.Increment(1)
            My.Computer.Audio.Play(My.Resources.audio.beatLight, AudioPlayMode.Background)

        Else
            beatProgressBar.Value = 0
            beatNum = 0
            My.Computer.Audio.Play(My.Resources.audio.beatHard, AudioPlayMode.Background) 'Change it to something diffrent laters
        End If

    End Sub

#End Region

    Private Function resetAll() As Boolean

        minsPlaying = 0
        secPlaying = 0

        beatNum = 0
        beatProgressBar.Value = 0

        timeTXTBox.Text = "0:0"

        Return True

    End Function

    Private Sub optionsBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optionsBtn.Click
        optionsWindow.ShowDialog()
    End Sub
End Class


' Audio mode enabling (depends on the radiobox selection)
'If modeBtnAudio.Checked = True Then
'mode = "audio"
'End If

' PC Speaker mode enabling (Depends on the same as above)
'If modeBtnSpeaker.Checked = True Then
'mode = "beep"
'End If